package logic

import (
	"context"
	"fmt"
	"user_srv/models"
	"user_srv/pkg"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuthRealNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAuthRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthRealNameLogic {
	return &AuthRealNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 实名验证
func (l *AuthRealNameLogic) AuthRealName(in *user.AuthRealNameRequest) (*user.AuthRealNameResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	name := models.AuthRealName{
		UserId:   int(in.UserId),
		RealName: in.RealName,
		IdCard:   in.IdCard,
	}
	if err = name.AuthList(); err != nil {
		logx.Error("实名失败")
		return nil, fmt.Errorf("实名失败")
	}
	if name.ID > 0 {
		logx.Error("用户已实名")
		return nil, fmt.Errorf("用户已实名")
	}
	if !pkg.Auth(in.IdCard, in.RealName) {
		logx.Error("第三方用户调用接口失败")
		return nil, fmt.Errorf("第三方用户调用接口失败")
	}
	if err = name.AuthCreate(); err != nil {
		logx.Error("用户已创建")
		return nil, fmt.Errorf("用户已创建")
	}

	return &user.AuthRealNameResponse{}, nil
}
